package com.wcity.manage.web.property;

import com.wcity.manage.common.bean.RetBean;
import com.wcity.manage.common.context.Constants;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.common.exception.CustomException;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.PropertyPay;
import com.wcity.manage.service.property.PropertyPayService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 前端物业费查询
 * Created by zm on 2016/12/20.
 */
@Controller
@RequestMapping("v1")
public class IPropertyPayAct {

    @Resource private PropertyPayService propertyPayService;

    /**
     * 物业费查询登入
     * @param payUser
     * @param payUserTel
     * @param
     * @return
     */
    @RequestMapping ("i_pay_login")
    @ResponseBody
    public RetBean IPayLogin ( String payUser , String payUserTel ) {

        if(StringUtil.isBlank(payUser) || StringUtil.isBlank(payUserTel) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        int count = propertyPayService.selectCount ( payUser , payUserTel  );
        if(count == 0){
            String errorMsg = "输入用户信息有误，请重新输入";
            return new RetBean ( Code.ERROR, errorMsg );
        }else{
            return new RetBean( Code.SUCCESS, "查询成功" );
        }
    }

    /**
     * 获取一年的账单
     * @param pageModel
     * @param payUser
     * @param payUserTel
     * @param payFeeNum
     * @return
     */
    @RequestMapping ("i_pay_list")
    @ResponseBody
    public Map<String,Object> IPayList (PageModel pageModel, String payUser , String payUserTel , String payFeeNum ,String year ) {

        Map<String,Object> map = new HashMap<>();
        if(StringUtil.isBlank(payUser) || StringUtil.isBlank(payUserTel) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        PropertyPay pay = new PropertyPay();
        pay.setPayUser( payUser );
        pay.setPayUserTel( payUserTel );
        pay.setPayFeeNum( payFeeNum );
        pay.setId( Integer.parseInt(year) );
        pageModel.setParams( pay );
        try {
            map.put("payList",propertyPayService.getIPayList(pageModel));
            map.put("retCode", Code.SUCCESS.getCodeNum());
            map.put("retDesc","请求成功");
        }catch ( Exception e ){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }

    /**
     * 获取一年的费用
     * @param payUser
     * @param payUserTel
     * @param year
     * @return
     */
    @RequestMapping ("i_pay_fee")
    @ResponseBody
    public Map<String,Object> IPayFee ( String payUser , String payUserTel ,String year ) {

        Map<String,Object> map = new HashMap<>();
        if( StringUtil.isBlank( payUser ) || StringUtil.isBlank( payUserTel )  || StringUtil.isBlank( year ) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        try {
            PropertyPay pay = propertyPayService.getPayFee ( payUser , payUserTel , year );
            map.put("PayMoney",pay == null ? "0":pay.getPayMoney());
            map.put("retCode", Code.SUCCESS.getCodeNum());
            map.put("retDesc","请求成功");
        }catch ( Exception e ){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }

    /**
     * 获取物业费账单详情
     * @param payId
     * @return
     */
    @RequestMapping ("i_pay_info")
    @ResponseBody
    public Map<String,Object> IPayInfo ( String payId ) {
        Map<String,Object> map = new HashMap<>();
        if(StringUtil.isBlank( payId )) throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        try {
            map.put("IPayInfo",propertyPayService.getIPayInfo ( payId ));
            map.put("retCode", Code.SUCCESS.getCodeNum());
            map.put("retDesc","请求成功");
        }catch ( Exception e ){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        return map;
    }

    /**
     * 获取缴费账单表
     * @param pageModel
     * @param payUser
     * @param payUserTel
     * @param type
     * @return
     */
    @RequestMapping ("years_list")
    @ResponseBody
    public Map<String ,Object > yearsList ( PageModel pageModel, String payUser , String payUserTel , String type ) {
        if( StringUtil.isBlank( payUser ) || StringUtil.isBlank( payUserTel )  || StringUtil.isBlank( type ) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        Map<String ,Object > map = propertyPayService.getYeanList(pageModel,payUser,payUserTel,type);
        return map;
    }
}
